import * as React from 'react';
import { Route, withRouter } from 'react-router-dom';
// 组件
import TabBar from '../component/TabBar'

class ChechAuth extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
          selectedTab: 'read',
          hidden: false
        }
        this.changeTab = this.changeTab.bind(this)
    }
    // 路由跳转
    changeTab (name) {
        this.setState({
        selectedTab: name
        })
    }
    render(){
        const { location, config } = this.props;
        const { pathname } = location;
        
        // 判断用户是否登录
        // const isLogin = localStorage.getItem('__config_center_token')

        // 路由是否匹配到值
        const targetRouterConfig = config.find((v) => v.path === pathname);

        // 做权限检测
        // if(targetRouterConfig && !targetRouterConfig.login){
        //     const { component } = targetRouterConfig;
        //     return <Route exact path={pathname} component={component} />
        // }

        // if(isLogin){
        //     // 如果路由合法，就跳转到相应的路由
        //     if(targetRouterConfig){
        //         return <Route path={pathname} component={targetRouterConfig.component} />
        //     }else{
        //         // 如果路由不合法，重定向到 404 页面
        //         return <Redirect to='/' />
        //     }
            
        // }else{
        //     return <Redirect to='/' />
        // }
        if (targetRouterConfig.hiddenTabBar) {
            return <Route exact path={pathname} component={targetRouterConfig.component} />
        } else {
            return (
                <div>
                    <Route exact path={pathname} component={targetRouterConfig.component} />
                    <TabBar {...this.state} changeTab={this.changeTab}></TabBar>
                </div>
            )
        }
        
        
    }
}

export default withRouter(ChechAuth)